//
//  RentHouseTopView.m
//  Liuhe
//
//  Created by zr on 2018/8/20.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "RentHouseTopView.h"


@interface RentHouseTopView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)UIButton * cancleButton;
@property (nonatomic, strong)UIButton * sureButton;
@property (nonatomic, strong)NSArray * styleArr;
@property (nonatomic, strong)NSArray * moneyArr;
@property (nonatomic, strong)NSArray * localArr;
@property (nonatomic, assign)NSInteger row;
@property (nonatomic, copy)NSString * string;

@end

@implementation RentHouseTopView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    self.backgroundColor = WHITE_COLOR;
    self.userInteractionEnabled = YES;
    self.styleArr = @[@"不限",@"1室",@"2室",@"3室",@"4室",@"5室"];
    self.moneyArr = @[@"不限",@"1000以下",@"1000-1500",@"1500-2000",@"2000-2500",@"2500以上"];
    self.localArr = @[@"不限",@"市内城区",@"锦江区",@"武侯区",@"青羊区",@"高新区"];
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,280 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 45 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"RentHouseTopViewCell"];
    
    [self addSubview:self.tableView];
    
    
    UIButton * cancelButton = [UIButton new];
    
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:BLACK_COLOR forState:UIControlStateNormal];
    [cancelButton setBackgroundColor:WHITE_COLOR];
    [cancelButton addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
 
    self.cancleButton = cancelButton;
    
    [self addSubview:self.cancleButton];
    
    
    UIButton * sureButton = [UIButton new];
    
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [sureButton setBackgroundColor:MAINCOLOR];
    [sureButton addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    
    self.sureButton = sureButton;
    
    [self addSubview:self.sureButton];
}


- (void)layoutSubviews{
    
    [self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self).offset(20 * SCREENH6);
        make.top.mas_equalTo(self.tableView.mas_bottom).offset(5 * SCREENH6);
        make.height.mas_equalTo(40 * SCREENH6);
        make.width.mas_equalTo(160 * SCREENH6);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.cancleButton.mas_trailing).offset(13 * SCREENH6);
        make.centerY.height.width.mas_equalTo(self.cancleButton);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.styleArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:@"RentHouseTopViewCell"];
    
    if (cell==nil) {
        
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RentHouseTopViewCell"];
    }
    
    cell.imageView.hidden = YES;
    
    if ([self.type isEqualToString:@"two"]) {
        
        cell.textLabel.text = self.moneyArr[indexPath.row];
        
    }else if ([self.type isEqualToString:@"three"]){
        
        cell.textLabel.text = self.styleArr[indexPath.row];
    }else if ([self.type isEqualToString:@"one"]){
        
        cell.textLabel.text = self.localArr[indexPath.row];
    }
    
    
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.textLabel.textColor = MAINCOLOR;
    self.string = cell.textLabel.text;
    
    self.row = indexPath.row;
}



- (void)clickBottomButton:(UIButton *)button{
    
    NSString * style;
    
    if (button == self.cancleButton) {
        
        style = @"cancle";
    }else if(button == self.sureButton){
        
        style = @"sure";
        
    }
    
    NSDictionary * dict;
    if (self.string) {
        
        dict = @{@"type":style,@"string":self.string};
    }else{
        
        dict = @{@"type":style};
    }
    
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RemoveTopTableView" object:nil userInfo:dict];
}


@end
